﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Threading;
using System.IO;
using System.IO.Ports;
using System.Net.Sockets;
using System.Net;
using System.Runtime.InteropServices;

namespace TTCaller
{
    public enum SendServerState { Idle, WaitACK, Disconnect, InitialGPRS };
    public class CmdTalker
    {

        [DllImport("coredll.dll", SetLastError = true)]
        private static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
        [DllImport("coredll.dll", SetLastError = true)]
        protected static extern int waveOutSetVolume(IntPtr device, uint volume);
        [DllImport("coredll.dll", SetLastError = true)]
        private static extern bool CloseHandle(IntPtr hObject);
        [DllImport("coredll.dll", SetLastError = true)]
        private static extern int CreateProcess(string imageName, string cmdLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, int boolInheritHandles, int dwCreationFlags, IntPtr lpEnvironment, IntPtr lpszCurrentDir, byte[] si, ProcessInfo pi);
        [DllImport("coredll.dll", SetLastError = true)]
        private static extern int GetExitCodeProcess(IntPtr hProcess, ref int lpExitCode);

        char[] cMsgSeq = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        int nMsgSeqIndex = 0;


        SerialPort m_pGPS;
        
        SerialPort m_pGPRS;
        string m_strATResult = "";



        Queue m_qRecvServer;
        Queue m_qSendServer;
        string m_strLastSendServer = "";
        DateTime m_dtWaitACKDT = System.DateTime.Now;
        Socket m_sktClient;
        SendServerState m_ssState = SendServerState.Disconnect;
        string m_strIMEI = StoreData.getInstance().IMEI;
        bool m_bFirstIP = false;

        static private CmdTalker m_Instance = new CmdTalker();
        private CmdTalker()
        {
            //initial(m_strCOMPORT);
            ResetServerCheckTimer();
        }

        static public CmdTalker getInstance()
        {
            return m_Instance;
        }


        private void OpenATCMDPort()
        {
            LogManager.getInstance().Log("OpenCOM2");
            m_pGPRS = new SerialPort("COM2", 115200, Parity.None, 8, StopBits.One);
            m_pGPRS.ReadTimeout = 500;
            m_pGPRS.WriteTimeout = 500;
            m_pGPRS.DataReceived += new SerialDataReceivedEventHandler(ATCMD_DataReceived);
            m_pGPRS.Open();
            m_pGPRS.DtrEnable = true;
            m_pGPRS.RtsEnable = true;
            LogManager.getInstance().Log("OpenCOM2 OK");

        }
        private void ClosATCMDPort()
        {
            LogManager.getInstance().Log("CloseCOM2");

            m_pGPRS.DataReceived -= new SerialDataReceivedEventHandler(ATCMD_DataReceived);
            m_pGPRS.Close();
            LogManager.getInstance().Log("CloseCOM2 OK");

        }
        private void ATCMD_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                m_strATResult = m_strATResult + m_pGPRS.ReadExisting();
            }
            catch (Exception ex)
            {
                LogManager.getInstance().Log("ATCMD Ex.", ex);
            }
        }
        private string RunATCMD(string arg_strCMD, int arg_nTimeout)
        {
            LogManager.getInstance().Log("RunATCMD:" + arg_strCMD); 
            string strTemp ="";
            try
            {
                m_strATResult = "";
                this.m_pGPRS.DiscardOutBuffer();
                this.m_pGPRS.DiscardInBuffer();
                this.m_pGPRS.Write(arg_strCMD + "\r");
                strTemp = this.ReadResponse(arg_nTimeout);
                if ((strTemp.Length == 0) || !strTemp.EndsWith("\r\nOK\r\n"))
                {
                    strTemp = "ERR";
                }
            }
            catch (Exception ex)
            {
                LogManager.getInstance().Log("RunATCMD Error:" + arg_strCMD, ex);
                strTemp = "ERR";
            }
            return strTemp;
        }
        private string ReadResponse(int arg_nTimeout)
        {
            DateTime dtWait = System.DateTime.Now.AddMilliseconds(arg_nTimeout);
            do
            {
                System.Windows.Forms.Application.DoEvents();
                Thread.Sleep(10);
            }
            while (!m_strATResult.EndsWith("\r\nOK\r\n") && !m_strATResult.EndsWith("\r\nERROR\r\n") && dtWait > System.DateTime.Now);
            return m_strATResult;
        }

        Thread m_thdInitialGPRS = null;
        private void initialGPRS()
        {
            if (m_ssState == SendServerState.InitialGPRS)
                return;
            setConnState(SendServerState.InitialGPRS);
            /*
            Form_Main.Main.ShowWaitingPanel();
            try
            {
                doGPRSInitial();
                
            }
            catch (Exception ex)
            {

            }
            Form_Main.Main.HideWaitingPanel();
            return;
             */
            LogManager.getInstance().Log("Initial GPRS Thread Start..");

            try
            {
                if (m_thdInitialGPRS != null)
                {
                    m_thdInitialGPRS.Abort();
                    LogManager.getInstance().Log("Initial GPRS Thread Abort..");
                }

                
                {
                    LogManager.getInstance().Log("Initial GPRS Thread new Start..");
                    m_thdInitialGPRS = new Thread(new ThreadStart(doGPRSInitial));
                    m_thdInitialGPRS.Start();

                }
            }
            catch (Exception ex)
            {

            }
            

        }

       // public void SetSendServerState(SendServerState arg_sss)
        //{
       //     m_ssState = arg_sss;
       // }



        public void doATCMDInitial()
        {

            bool bCMDOK = false;
            int nTryCMD = 0;
            do
            {
                try
                {
                    bCMDOK = false;
                    LogManager.getInstance().Log("InitATCMD:" + nTryCMD.ToString());

                    //TODO:任何失敗, 都要等候重
                    string strTemp = "";
                    OpenATCMDPort();
                    strTemp = this.RunATCMD("AT", 500);
                    if (strTemp != "ERR")
                    {
                        strTemp = this.RunATCMD("ATE0", 500);
                        if (strTemp != "ERR")
                        {
                            if (m_strIMEI == "")
                                m_strIMEI = StoreData.getInstance().IMEI;

                            if (m_strIMEI == "")
                            {
                                strTemp = this.RunATCMD("AT+CGSN", 500);
                                if (strTemp != "ERR")
                                {
                                    string str = "";
                                    str = strTemp.Substring(strTemp.IndexOf("\r\n") + 2, 15);
                                    if (str.Length == 15)
                                    {
                                        LogManager.getInstance().Log("GII:" + str);

                                        StoreData.getInstance().IMEI = str;
                                    }
                                }
                                else
                                {
                                    LogManager.getInstance().Log("AT+CGSN ER");

                                }
                            }

                            strTemp = this.RunATCMD("AT+CPIN=\"0925\"", 2500);
                            if (strTemp == "ERR")
                            {
                                LogManager.getInstance().Log("AT+CPINER");
                            }
                            strTemp = "";


                        }
                        else
                        {
                            LogManager.getInstance().Log("A0ER");

                        }
                    }
                    else
                    {
                        LogManager.getInstance().Log("ATER");

                    }
                    if (strTemp != "ERR")
                    {
                        m_strIMEI = StoreData.getInstance().IMEI;
                        LogManager.getInstance().Log("II:" + m_strIMEI);                        
                        bCMDOK = true;
                    }
                    else
                    {
                        LogManager.getInstance().Log("GIIER." + m_strIMEI + ":" + StoreData.getInstance().IMEI);
                    }
                    //S3000GGSMModem sm = new S3000GGSMModem();
                }
                catch (Exception ex)
                {
                    LogManager.getInstance().Log("ATCMDER:" + ex.Message + ex.StackTrace, false);
                }
                finally
                {
                    if (this.m_pGPRS != null)
                    {
                        this.ClosATCMDPort();
                        this.m_pGPRS = null;
                    }
                }
                
                if (!bCMDOK)
                {
                    DateTime dtWaitRESET = System.DateTime.Now.AddSeconds(3);
                    try
                    {
                        while (dtWaitRESET < System.DateTime.Now)
                        {
                            Thread.Sleep(100);
                            System.Windows.Forms.Application.DoEvents();
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.getInstance().Log("ATCMDSLPER:" + ex.Message + ex.StackTrace, false);
                    }
                }
                nTryCMD += 1;
                if (nTryCMD > 2)
                {
                    LogManager.getInstance().Log("TRCMDER.", false);
                    nTryCMD = 0;
                    bCMDOK = true;
                }
                
            }
            while (!bCMDOK);
            LogManager.getInstance().Log("InitATCMDOK");

        }

        public void doGPRSInitial()
        {
            try
            {
                int nTemp = m_nConnErrorCount;
                if (nTemp < 3)
                    nTemp = 0;
                Thread.Sleep(5000 * nTemp);
                if (checkConnectServer())
                {
                    LogManager.getInstance().Log("RCntSrOK.", new Exception("ReconnectOK"));
                    return;
                }

                //do
                {
                    //Thread.Sleep(1000);
                    doATCMDInitial();
                }
                //while (m_strIMEI == "");

                LogManager.getInstance().Log("InitGPRS.", new Exception("ReStartGPRS"));
                //read AT CMD Data


                LogManager.getInstance().Log("Prepare connect to Server");
                try
                {

                    int nTry = 0;
                    bool bConn = false;

                    while (nTry < 3 && !bConn)
                    {

                        //throw new Exception("Test Error...");
                        int nResult = 0;
                        while (nResult < 3)
                        {
                            System.Windows.Forms.Application.DoEvents();
                            if (StartProcess("\\SDMMC\\PNDSoftware\\FETAGPS.EXE", "") > 0)
                            {
                                Form_Main.Main.setBoxStatus(BoxStatusItem.GPRS, true);
                                LogManager.getInstance().Log("Connect GPRS OK..");
                                break;
                            }
                            nResult += 1;
                            LogManager.getInstance().Log("re-try FETAGPS", false);
                        }

                        int nConnTry = 0;
                        while (nConnTry < 1 && !bConn)
                        {

                            if (checkConnectServer())
                            {
                                bConn = true;

                                break;
                            }
                            nConnTry += 1;
                            DateTime dtTemp = System.DateTime.Now.AddSeconds(5);
                            while (dtTemp > System.DateTime.Now)
                            {
                                System.Windows.Forms.Application.DoEvents();
                                Thread.Sleep(200);
                            }
                        }
                        LogManager.getInstance().Log("re-try Connect GPRS:" + nTry.ToString());
                        nTry += 1;
                    }
                }
                catch (Exception ex)
                {
                    LogManager.getInstance().Log("Error Connect Server:" + StoreData.getInstance().ServerIP1 + "@" + ex.Message + "@" + ex.StackTrace, false);
                }

                if (m_ssState == SendServerState.Idle)
                {
                    LogManager.getInstance().Log("Connect to Server OK");
                }
                else if (m_ssState == SendServerState.InitialGPRS)
                {
                    LogManager.getInstance().Log("Initial Fail Disconnect..");

                    setConnState(SendServerState.Disconnect);
                }
            }
            catch (Exception ex)
            {
                LogManager.getInstance().Log("EXDs:"+ ex.Message);

                setConnState(SendServerState.Disconnect);
            }

        }

        private bool checkConnectServer()
        {

            string[] strServer = StoreData.getInstance().ServerIP1.Split(',');
            try
            {
                char c = m_strIMEI[m_strIMEI.Length - 1];
                int n = System.Convert.ToInt16(c);
                if (m_bFirstIP) n += 1;

                if (n % 2 == 0)
                {
                    strServer = StoreData.getInstance().ServerIP2.Split(',');
                }

                m_bFirstIP = !m_bFirstIP;
                LogManager.getInstance().Log("ServerIP:" + System.Convert.ToString(n % 2 == 0)+ ":" + strServer[0] + "," + strServer[1], false);


            }
            catch (Exception ex)
            {
                LogManager.getInstance().Log("Error ServerID:" + strServer[0] + "," + strServer[1] + "@" + ex.Message, false);

            }

            try
            {
                //initial Socket
                //TODO:改成支援多IP機制

                //                IPEndPoint rep = new IPEndPoint(IPAddress.Parse("59.126.162.8"), 9113
                IPEndPoint rep = new IPEndPoint(IPAddress.Parse(strServer[0]), System.Convert.ToInt32(strServer[1]));//System.Convert.ToInt32(9113));
                m_sktClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                m_sktClient.Connect(rep);
                setConnState(SendServerState.Idle);

                Form_Main.Main.setBoxStatus(BoxStatusItem.Server, true);
                Form_Main.Main.setBoxStatus(BoxStatusItem.GPRS, true);


                //送出 IMEI
                LogManager.getInstance().Log("SendIMEI:" + m_strIMEI, false);
                AddToSendServer(m_strIMEI);
                LogManager.getInstance().Log("ReLoginCM:" + "ST" + StoreData.getInstance().TEL, false);
                AddToSendServer("ST" + StoreData.getInstance().TEL+GetMsgSeq());
                //Form_Main.Main.ReLogin();

                return true;
            }
            catch (Exception ex2)
            {
                LogManager.getInstance().Log("Error Connect Server:" + strServer[0] + "," + strServer[1] + "@" + ex2.Message, false);
            }
            finally
            {
                //m_sktClient = null;
            }
            return false;

        }

        public void initial()
        {
            m_qRecvServer = new Queue();
            m_qSendServer = new Queue();


            initialGPRS();



        }


        public class ProcessInfo
        {
            // Fields
            public IntPtr hProcess;
            public IntPtr hThread;
            public IntPtr ProcessID;
            public IntPtr ThreadID;
        }

        public int StartProcess(string strPath, string strParms)
        {
            int num = 0;
            try
            {
                ProcessInfo pi = new ProcessInfo();
                byte[] si = new byte[0x80];
                num = CreateProcess(strPath, strParms, IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, si, pi);
                WaitForSingleObject(pi.hProcess, uint.MaxValue);
                int lpExitCode = 0;
                lpExitCode = GetExitCodeProcess(pi.hProcess, ref lpExitCode);
               // System.Windows.Forms.MessageBox.Show("Connect GPRS ExitCode:" + lpExitCode.ToString());

                CloseHandle(pi.hProcess);
                CloseHandle(pi.hThread);
            }
            catch (Exception exception)
            {
                //System.Windows.Forms.MessageBox.Show(exception.Message + "\r\n" + exception.StackTrace, "SrtPro");
                System.Windows.Forms.Application.DoEvents();
            }
            return num;
        }

 
        byte[] baryRecv = new byte[500];
        DateTime m_dtLastSend = System.DateTime.Now;
        DateTime m_dtLastSendWaitMax = System.DateTime.Now;
        int m_nRecvIndex = 0;

        int m_nConnErrorCount = 0;

        DateTime m_dtLastGPRSCheck = System.DateTime.Now.AddMilliseconds(100);
        public void doGPRSCheck()
        {
            if (m_dtLastGPRSCheck > System.DateTime.Now)
            {
                return;
            }

            m_dtLastGPRSCheck = System.DateTime.Now.AddMilliseconds(100);

            switch (m_ssState)
            {
                case SendServerState.Idle:
                    CheckNeedSendToServer();
                    break;
                case SendServerState.WaitACK:
                    if (m_dtWaitACKDT < System.DateTime.Now)
                    {
                        LogManager.getInstance().Log("WaitAckDisconnect..");
                        setConnState(SendServerState.Disconnect);
                    }
                    break;
                case SendServerState.Disconnect:
                    LogManager.getInstance().Log("DisIn");
                    m_dtLastGPRSCheck = System.DateTime.Now.AddSeconds(5);
                    if (m_nConnErrorCount < 10)
                        m_nConnErrorCount += 1;
                    Form_Main.Main.m_bLogined = false;
                    Form_Main.Main.setBoxStatus(BoxStatusItem.Server, false);
                    Form_Main.Main.setBoxStatus(BoxStatusItem.GPRS, false);                    
                    
                    initialGPRS();

                    break;
            }

            //checkRecv
            try
            {
                if (m_ssState == SendServerState.Idle || m_ssState == SendServerState.WaitACK)
                {
                    if (m_sktClient != null && m_sktClient.Connected)
                    {
                        if (m_sktClient.Available > 0)
                        {
                            Form_Main.Main.setBoxStatus(BoxStatusItem.Server, true);
                            while (m_sktClient.Available > 0)
                            {
                                m_sktClient.Receive(baryRecv, m_nRecvIndex++, 1, SocketFlags.None);

                                if (m_nRecvIndex > 2 && baryRecv[m_nRecvIndex - 2] == '\r' && baryRecv[m_nRecvIndex - 1] == '\n')
                                {
                                    break;
                                }

                                if (m_nRecvIndex > 495)
                                    m_nRecvIndex = 0;
                            }
                            //string strTemp = System.Text.Encoding.Default.GetString(baryRecv, 0, m_nRecvIndex);

                            //LogManager.getInstance().Log("Recv Data.." + strTemp + ":" + strTemp.Length, true);

                            if (m_nRecvIndex > 2 && baryRecv[m_nRecvIndex - 2] == '\r' && baryRecv[m_nRecvIndex - 1] == '\n')
                            {
                                m_nConnErrorCount = 0;
                                ResetServerCheckTimer();
                                string strTemp = System.Text.Encoding.GetEncoding("big5").GetString(baryRecv, 0, m_nRecvIndex-2);
                                LogManager.getInstance().Log("Recv Svr:" + strTemp + ":" + strTemp.Length);
                                //if (strTemp.IndexOf("AT$SNDTXT=") == 0 )
                                {
                                    LogManager.getInstance().Log("RecvSrvCMD:" + strTemp);
                                    switch (strTemp[0])
                                    {
                                        case '0':
                                        case '1':
                                        case '2':
                                        case '3':
                                        case '4':
                                        case '5':
                                        case '6':
                                        case '7':
                                        case '8':
                                        case '9':
                                            if (m_ssState == SendServerState.WaitACK)
                                            {
                                                LogManager.getInstance().Log("ACK Recv.." + strTemp);
                                                setConnState(SendServerState.Idle);
                                            }
                                            break;
                                        case 'E':
                                            break;
                                        case '$':
                                            SendToServerConnectCheck(strTemp);
                                            break;

                                        default:
                                            LogManager.getInstance().Log("Enqueue:" + strTemp);
                                            lock (m_qRecvServer)
                                            {
                                                m_qRecvServer.Enqueue(strTemp);
                                            }

                                            //SendBack ACK
                                            SendToServerUIACK();

                                            break;

                                    }
                                }

                                m_nRecvIndex = 0;
                            }

                        }
                    }
                    else
                    {
                        LogManager.getInstance().Log("Connect Broken Disconnect..");

                        setConnState(SendServerState.Disconnect);
                        
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.getInstance().Log("CheckRecv Error", ex);
                LogManager.getInstance().Log("CheckRecv Exception Disconnect..");
                setConnState(SendServerState.Disconnect);
            }

            if (m_ssState == SendServerState.Idle && m_dtLastServerCheck < System.DateTime.Now)
            {
                ResetServerCheckTimer();
                LogManager.getInstance().Log("SC Dis.");
                setConnState(SendServerState.Disconnect);
            }

        }

        private void AddToSendServer(string arg_strData)
        {
            lock (m_qSendServer)
            {
                m_qSendServer.Enqueue(arg_strData+ "\r\n");
            }
        }

        public string checkServerCMD(string arg_strHeader)
        {
            lock (m_qRecvServer)
            {
                int nCount = m_qRecvServer.Count;

                for (int i = 0; i < nCount; i++)
                {
                    string strTemp = (string)m_qRecvServer.Dequeue();
                    //LogManager.getInstance().Log("check ServerCmd ->" + arg_strHeader + ":" + strTemp);

                    if (strTemp.Length >= arg_strHeader.Length && strTemp.Substring(0, arg_strHeader.Length) == arg_strHeader)
                    {

                        LogManager.getInstance().Log("check ServerCmd OK ->" + arg_strHeader + ":" + strTemp);
                        return strTemp;
                    }
                    else
                    {
                        m_qRecvServer.Enqueue(strTemp);
                    }
                }
            }
            return "";
        }
        private void CheckNeedSendToServer()
        {
            lock (m_qSendServer)
            {
                if (m_qSendServer.Count > 0)
                {
                    try
                    {
                        m_strLastSendServer = (string)m_qSendServer.Dequeue();
                        if (m_strLastSendServer != null)
                        {
                            LogManager.getInstance().Log("CkSSr:" + m_strLastSendServer);

                            byte[] barySend = System.Text.Encoding.UTF8.GetBytes(m_strLastSendServer.ToString());
                            m_sktClient.Send(barySend);
                            m_dtWaitACKDT = System.DateTime.Now.AddSeconds(10);
                            setConnState(SendServerState.WaitACK);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.getInstance().Log("CkSSrExDs..");
                        setConnState(SendServerState.Disconnect);
                        LogManager.getInstance().Log("SSrEr", ex);
                    }
                }

            }
        }

        private void setConnState(SendServerState arg_ss)
        {
            //lock (m_ssState)
            {
                LogManager.getInstance().Log("SetSts"+ arg_ss.ToString());

                m_ssState = arg_ss;
            }
        }

        public string SendServerAndWaitResponse(string arg_strCmd, int arg_nWaitSecs, string arg_strWaitString, bool arg_bShowWaitingPanel)
        {
            DateTime dtMinTime = System.DateTime.Now.AddMilliseconds(1000);
            Form_Main.Main.updateLastServerCheck();
            if(arg_bShowWaitingPanel)
                Form_Main.Main.ShowWaitingPanel();

            //m_strLastRecvACK = "";

            CmdPack cp = new CmdPack(arg_strCmd, System.DateTime.Now.Add(new TimeSpan(0, 0, arg_nWaitSecs)));

            try
            {
                try
                {
                    LogManager.getInstance().Log("SSrW:" + cp.m_strCMD);

                    //prepare String

                    AddToSendServer(arg_strCmd+GetMsgSeq());

                }
                catch (Exception ex)
                {
                    LogManager.getInstance().Log("SCmER:" + arg_strCmd, ex);
                }
                while (cp.m_dtWait > System.DateTime.Now)
                {
                    if (arg_strWaitString == "MOK")
                    {
                        if (m_ssState == SendServerState.Idle)
                        {
                            cp.m_strResponse = "MOK";
                            break;
                        }
                    }
                    else
                    {
                        string strTemp = CmdTalker.getInstance().checkServerCMD(arg_strWaitString);
                        if (strTemp != "")
                        {
                            cp.m_strResponse = strTemp;
                            break;
                        }
                    }

                    System.Threading.Thread.Sleep(100);
                    System.Windows.Forms.Application.DoEvents();
                }
                while (dtMinTime > System.DateTime.Now)
                {
                    System.Threading.Thread.Sleep(100);
                    System.Windows.Forms.Application.DoEvents();
                }
            }
            catch (Exception ex)
            {
                LogManager.getInstance().Log("SSrEx", ex);
            }

            if (arg_bShowWaitingPanel)
                Form_Main.Main.HideWaitingPanel();

            return cp.m_strResponse;
        }


        private char GetMsgSeq()
        {
            char cResult = cMsgSeq[nMsgSeqIndex];
            nMsgSeqIndex = (nMsgSeqIndex + 1) % 10;
            return cResult;
        }

        private void SendToServerUIACK()
        {
            byte[] barySend = System.Text.Encoding.UTF8.GetBytes(GetMsgSeq() + "\r\n");
            m_sktClient.Send(barySend);

        }

        public void UpdateSystem()
        {
            ResetServerCheckTimer();

        }


        public int m_nServerCheckMins = 4;
        DateTime m_dtLastServerCheck;



        public void ResetServerCheckTimer()
        {
            m_dtLastServerCheck = System.DateTime.Now.AddMinutes(m_nServerCheckMins);
        }
        private void SendToServerConnectCheck(string arg_str)
        {
            LogManager.getInstance().Log("SendBack:"+arg_str, false);
            byte[] barySend = System.Text.Encoding.UTF8.GetBytes(arg_str+"\r\n");
            m_sktClient.Send(barySend);
            ResetServerCheckTimer();
        }

    }

    public class CmdPack
    {
        public string m_strCMD;
        public string m_strResponse="";
        public DateTime m_dtWait = System.DateTime.Now.Add(new TimeSpan(0,0,10));
        public bool m_bResult = false;
        public CmdPack(string arg_strCMD)
        {
            m_strCMD = arg_strCMD;
        }

        public CmdPack(string arg_strCMD, DateTime arg_dtWait)
        {
            m_strCMD = arg_strCMD;
            m_dtWait = arg_dtWait;

        }


    }
}
